package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.Vo.*;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.WxMethodService;
import org.apache.poi.ss.formula.functions.Now;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class WxMethodServiceImpl implements WxMethodService {

    @Autowired
    private SysNoticeMapper sysNoticeMapper;
    @Autowired
    private CmCourtBlockMapper cmCourtBlockMapper;
    @Autowired
    private CmUserInfoMapper cmUserInfoMapper;
    @Autowired
    private CmManagerInfoMapper cmManagerInfoMapper;
    @Autowired
    private CmUserHtclockinMapper cmUserHtclockinMapper;
    @Autowired
    private CmAnonymousComeMapper cmAnonymousComeMapper;
    @Autowired
    private CmAbnormalInfoMapper cmAbnormalInfoMapper;

    /**
     *
     * 公告接口 R
     * @return
     */
    public R selectNoticedesc(){
        return new R(200,"susscess!",sysNoticeMapper.selectNoticedesc());
    }

    /**
     *
     *  查询居民居住信息
     *
     */
    public R selectBlockInfo(String openid){ return  new R(200,"susscess!",cmCourtBlockMapper.selectBlockInfo(openid));}

//    /**
//     *
//     *  查询角色信息
//     *
//     */
//    public R selectRoleInfo(String openid){
//        return new R
//    }
    /**
     *
     *  查询社管联系方式
     *
     */
    public R selectManagerPhone(){
        return new R(200,"susscess",cmManagerInfoMapper.selectManagerPhoneList());
    }


    /**
     *
     *    提交打卡信息
     * @Pa CmUserHtclockin ,  NowDate,  openid
     *
     */
    @Transactional
    public R insertHty(CmUserHtDto cmUserHtDto){

        CmUserHtHty cmUserHtHty = cmUserHtclockinMapper.selectWxUserNowdate(cmUserHtDto);
        if(cmUserHtHty==null){
            int code = cmUserHtclockinMapper.insertHtDto(cmUserHtDto);
            if(code==1){
                return new R(200,"打卡成功");
            }else{
                return new R(400,"打卡失败");
            }
        }
            return new R(400,"不可重复打卡");
    }
    @Transactional
    public R selectWxUserInfo(String openid){
        CmUserInfo cmUserInfo = cmUserInfoMapper.selectCmUserInfoOpenId(openid);

        HashMap<String,Object> userInfo=new HashMap<>();
        userInfo.put("nickname",cmUserInfo.getNickname());
        userInfo.put("avatarUrl",cmUserInfo.getAvatarUrl());
        userInfo.put("realname",cmUserInfo.getRealname());
        userInfo.put("phone",cmUserInfo.getPhone());
        userInfo.put("idcard",cmUserInfo.getIdcard());
        if(cmUserInfo.getOwId()!=null){
            CmCourtVo cmCourtVo = cmCourtBlockMapper.selectBlockInfo(openid);
            if(cmCourtVo!=null){
                userInfo.put("AmName",cmCourtVo.getAm_name());
                userInfo.put("BlockName",cmCourtVo.getBlockName());
                return new R(200,"获取成功",userInfo);
            }
        }
        return new R(202,"获取成功，暂未绑定家庭地址",userInfo);

    }

    //修改个人信息
    public R updateWxUserInfo(CmUserInfo cmUserInfo){
        int i = cmUserInfoMapper.updateOpenIdInfo(cmUserInfo);
        if(i==1){
            return new R(200,"修改成功");
        }
        return new R(202,"修改失败");
    }

    //社管修改个人信息
    public R updateManagerInfo(CmManagerInfo cmManagerInfo){
        int i = cmManagerInfoMapper.updateCmManagerInfoByOpenid(cmManagerInfo);
        if(i==1){
            return new R(200,"修改成功");
        }
        return new R(202,"修改失败");
    }

    //添加家庭成员
    public R updateUserInfoBlock(String openid,String phone){
        CmUserInfo cmUserInfo = cmUserInfoMapper.selectCmUserInfoOpenId(openid);
        CmUserInfo user=new CmUserInfo();
        user.setPhone(phone);
        CmUserInfo adduser = cmUserInfoMapper.selectCmUserInfo(user);
        if(adduser!=null){
            adduser.setOwId(cmUserInfo.getOwId());
            int i = cmUserInfoMapper.updateOpenIdInfo(adduser);
            if(i==1){
                return new R(200,"修改成功");
            }
        }
        return new R(202,"修改失败,请确认手机号码");

    }

   //根据openid查询家庭成员
    public R selectBlockUser(String openid){
        CmUserInfo cmUserInfo = cmUserInfoMapper.selectCmUserInfoOpenId(openid);
        CmUserInfo Cmuser=new CmUserInfo();
        Cmuser.setOwId(cmUserInfo.getOwId());
        List<CmUserInfo> cmUserInfos = cmUserInfoMapper.selectCmUserInfoList(Cmuser);
        if(cmUserInfos.size()>0){
            return new R(200,"查询成功",cmUserInfos);
        }
        return new R(202,"查询失败");
    }

    /**
     *  访客登记表单
     *
     */
    public R AyMousComeSubmit( CmAnonymousCome cmAnonymousCome){
        CmUserInfo cmUserInfo = cmUserInfoMapper.selectCmUserInfoPhone(cmAnonymousCome.getPhone());
        if(cmUserInfo!=null) {
            cmAnonymousCome.setIsAgree(0);
            int i = cmAnonymousComeMapper.insertCmAnonymousCome(cmAnonymousCome);
            if (i == 1) {
                return new R(200, "提交成功");
            }
            return new R(202, "提交失败");
        }
        return new R(400,"被访问者错误");
    }

    /**
     *  访客登记记录
     *
     */
    public R AymousComeHistory(String openid){
        List<CmAnMComeHistoryVo> cmAnMComeHistoryVos = cmAnonymousComeMapper.selectCmAnonyMousHistroyList(openid);
        if(cmAnMComeHistoryVos.size()>0){
            return new R(200,"查询到"+cmAnMComeHistoryVos.size()+"条记录",cmAnMComeHistoryVos);
        }
        return new R(202,"未查询到相关记录");
    }

    /**
     *  根据居民openid查询当前待审批的访客
     *
     */
    public R selectApproveHistoryList(String openid){
        List<CmAnonymousCome> cmAnonymousComes = cmAnonymousComeMapper.selectApproveHistoryList(openid);
        if(cmAnonymousComes.size()>0){
            return new R(200,"查询到"+cmAnonymousComes.size()+"条记录",cmAnonymousComes);
        }
        return new R(202,"当前并无访客申请");
    }

    /**
     *
     *
     */
    @Transactional
    public R AnymousApprove(Long id,Integer isAgree){
        CmAnonymousCome cm = cmAnonymousComeMapper.selectCmAnonymousComeById(id);
        if(cm.getIsAgree()!=0){
            return new R(400,"不能修改");
        }
        CmAnonymousCome cmAnonymousCome=new CmAnonymousCome();
        cmAnonymousCome.setId(id);
        cmAnonymousCome.setIsAgree(isAgree);
        int i = cmAnonymousComeMapper.updateCmAnonymousCome(cmAnonymousCome);
        if(i==1){
            return new R(200,"修改完成");
        }
        return new R(202,"修改失败");
    }

    /**
     * 根据社管的openid查询社管个人信息
     * @param openid
     * @return CmManagerInfo
     */
    public R selectManagerInfo(String openid){
        CmManagerInfo cmManagerInfo = cmManagerInfoMapper.selectCmManagerByOpenid(openid);
        if(cmManagerInfo.getId()!=null){
            return new R(200,"查询成功!",cmManagerInfo);
        }
        return new R(202,"openid有误");
    }


    /**
     *
     * 居民异常求助提交
     * @param openid
     * @param remark
     * @return
     */
    public R abnormalSubmit(String openid,String remark){
        CmAbnormalInfo cmAbnormalInfo=new CmAbnormalInfo();
        cmAbnormalInfo.setOpenid(openid);
        cmAbnormalInfo.setUserRemark(remark);
        if(cmAbnormalInfoMapper.insertWxCmAbnormalInfo(cmAbnormalInfo)==1){
            return new R(200,"提交成功");
        }
        return new R(202,"提交错误");
    }

    /**
     *
     *  社管异常处理审批
     *
     */
    @Transactional
    public R abnormalApprove(Long id,String openid,String ManagerRemark,Integer Status){
        CmAbnormalInfo cmAbnormalInfo=new CmAbnormalInfo();
        cmAbnormalInfo.setId(id);
        cmAbnormalInfo.setManagerid(openid);
        cmAbnormalInfo.setManagerRemark(ManagerRemark);
        cmAbnormalInfo.setStatus(Status);
        int i = cmAbnormalInfoMapper.updateCmAbnormalInfo(cmAbnormalInfo);
        if(i==1){
            return new R(200,"提交成功");
        }else if(i==0){
            return new R(202,"提交失败");
        }
        return new R(202,"提交失败");
    }
}
